import React from 'react';
import DataTable from 'components/DataTable';
import Icon from 'components/Icon';
import Button from 'components/Button';
import { router } from 'dva';
import moment from 'moment';

const { Link } = router;

export default (self, lecturers, lectureType) => [
  {
    title: 'ID',
    name: 'id',
    formItem: {'type': 'hidden'}
  },
  {
    title: '名称',
    name: 'name',
    tableItem: {},
    formItem: {
      rules: []
    },
    searchItem: {
      group: 'abc'
    }
  },{
    title: '简介',
    name: 'description',
    tableItem: {},
    formItem: {
      rules: []
    },
  },{
    title: '类别',
    name: 'categoryId',
    tableItem: {render: text => {
      let temp =  lectureType.filter(item => item.id == text);
      if(temp && temp.length == 1){
        return temp[0]['name'];
      }else{
        return '未知'
      }
    }},
    dict: lectureType.map(item=> {return {code : item.id, codeName: item.name}}),
    formItem: {
      type: 'select',
      rules: []
    }
  },{
    title: '主讲老师',
    name: 'lecturerList',
    tableItem: {
      render: text => text.map(item => item.realname).join(',')
    },
    formItem: {
      type: 'transfer',
      modal: true,
      dataSource: lecturers,
      normalize: value => value.map(item => item.id),
      rules: []
    }
  },{
    title: '公开',
    name: 'isPublic',
    dict: [{ code: 0, codeName: '否' }, { code: 1, codeName: '是' }],
    tableItem: {
      // render: text => text ? '是': '否'
    },
    // formItem: {
    //   type: 'select'
    // },
  },
  // {
  //   title: '推荐',
  //   name: 'isRecommend',
  //   dict: [{ code: 0, codeName: '否' }, { code: 1, codeName: '是' }],
  //   tableItem: {
  //     // render: text => text ? '是': '否'
  //   },
  //   // formItem: {
  //   //   type: 'select'
  //   // },
  // },
  { title: '课程封面',
    name: 'cover',
    formItem: {
      type: 'upload',
      action: '/api/lesson/upload/lessonCover',
      fileName: 'file',
      fileTypes: ['.png', '.jpg', '.gif'],
      listType: 'picture',
      normalize: value => {
        return [
          {
            uid: -1,
            thumbUrl: value
          }
        ];
      },
      max:1,
      multiple: false,
      // showUploadList: false,
      rules: [
        {
          required: true,
          message: '请上传封面'
        }
      ]
    }
  },{
    title: '创建时间',
    name: 'createTime',
    tableItem: {
      render: text =>
        moment.utc(text).format('YYYY-MM-DD HH:mm')
        // moment.utc(text).toDate().toLocaleDateString()
    },
  },{
    title: '更新时间',
    name: 'updateTime',
    tableItem: {
      render: text =>
        moment.utc(text).format('YYYY-MM-DD HH:mm')
    },
  },

  {
    title: '操作',
    tableItem: {
      width: 180,
      render: (text, record) => {
        const publishBtn = record.isPublic ?
          <Button tooltip="下架" onClick={e => self.onPublish(record, 0)}>
            <Icon type="DownSquareOutlined" antd/>
          </Button> :
          <Button tooltip="上架" onClick={e => self.onPublish(record, 1)}>
            <Icon type="UpSquareOutlined" antd/>
          </Button>;
        // const recommendBtn = record.isRecommend ?
        //   <Button tooltip="撤销" onClick={e => self.onRecommend(record, 0)}>
        //     <Icon type="CloseSquareOutlined" antd/>
        //   </Button> :
        //   <Button tooltip="推荐" onClick={e => self.onRecommend(record, 1)}>
        //     <Icon type="CheckSquareOutlined" antd/>
        //   </Button>;
        return <DataTable.Oper>
          <Button tooltip="修改" onClick={e => self.onUpdate(record)}>
            <Icon type="edit"/>
          </Button>
          <Button tooltip="删除" onClick={e => self.onDelete(record)}>
            <Icon type="trash"/>
          </Button>
          {publishBtn}
          {/*{recommendBtn}*/}
          <Button tooltip="详情">
            <Link to={"/lessons/detail?id=" + record.id}>
              <Icon type="info"/>
            </Link>
          </Button>
        </DataTable.Oper>
      }
    }
  }
];
